/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package concreteBuilders;

import builderProduct.AbstractBuilder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Vector;
import operacoes2.Operacao;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Rente
 */
public class XMLFileBuilder extends AbstractBuilder {

    Vector<Operacao> vector;

    @Override
    public void buildCabecalho() {
        System.out.println("SOU XML -> Vou construir o cabeçalho");
        fb.setCabecalho("<?xml version=\"1.0\"?>");
    }

    @Override
    public void buildCorpo() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd#HH:mm:ss");

        vector = super.getVal();
        System.out.println("SOU XML -> Vou construir o corpo");
        Document doc = new Document();

        Element root = new Element("Operacoes");
        for (Operacao operacao : vector) {

            String stuff = operacao.toString();
            String[] s = stuff.split(" ");
            String op = s[0];
            Attribute data = new Attribute("Data", dateFormat.format(operacao.getData()));
            Element el = new Element(op);
            el.setAttribute(data);
            if (op.equals("Levantamento")) {
                Attribute quantia = new Attribute("Quantia", s[2]);
                Attribute conta = new Attribute("Conta", s[6]);
                el.setAttribute(quantia);
                el.setAttribute(conta);
            }else if (op.equals("Carregamento")){
                Attribute valor = new Attribute("Valor", s[2]);
                Attribute cartao = new Attribute("Cartao", s[6]);
                Attribute conta = new Attribute("Conta", s[9]);
                el.setAttribute(valor);
                el.setAttribute(cartao);
                el.setAttribute(conta);
            }else if(op.equals("Deposito")){
                Attribute quantia = new Attribute("Quantia", s[2]);
                Attribute conta = new Attribute("Conta", s[6]);
                el.setAttribute(quantia);
                el.setAttribute(conta);
            }else if(op.equals("PIN")){
                Attribute cartao = new Attribute("Cartao", s[3]);
                el.setAttribute(cartao);            
            }else if(op.equals("Pagamento")){
                Attribute quantia = new Attribute("Quantia", s[2]);
                Attribute referencia = new Attribute("Referencia", s[6]);
                System.out.println("SIZE:" + s.length);
                Attribute conta = new Attribute("Conta", s[10]);
                el.setAttribute(quantia); 
                el.setAttribute(referencia); 
                el.setAttribute(conta);                
            }else if(op.equals("Transferencia")){
                Attribute quantia = new Attribute("Quantia", s[2]);
                Attribute contaOrigem = new Attribute("ContaOrigem",s[5]);
                Attribute contaDestino = new Attribute("ContaDestino",s[9]);
                el.setAttribute(quantia);
                el.setAttribute(contaOrigem);
                el.setAttribute(contaDestino);
            }
            root.addContent(el);
        }
        doc.setRootElement(root);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        fb.setCorpo(new StringBuffer(outputter.outputString(doc)));

    }

    @Override
    public void buildExtensao() {
        fb.setExtensao("xml");
    }
}
